//
//  ConnectWithPhoneViewController.m
//  SmartCoin
//
//  Created by apple on 2018/3/14.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#pragma mark--field限制还没写
#import "ConnectWithPhoneViewController.h"

@interface ConnectWithPhoneViewController ()
@property (nonatomic ,retain)UIButton *rulebutton;
@property (nonatomic ,retain)UITextField *PhoneField;
@property (nonatomic ,retain)UITextField *CodeFeild;
@property (nonatomic ,retain)UITextField *PassFeild;
@property (nonatomic ,retain)UIButton *timeButton;
@property (nonatomic ,assign)NSInteger timeNum;
@end

@implementation ConnectWithPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bggrayColor;
    [self sureLoad];
    [self viewLoad];
    
}

-(void)sureLoad{
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(cx_wid / 2- 127.5, 263, 255, 40)];
    //    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    CAGradientLayer *layer = [[CAGradientLayer alloc] init];
    layer.frame = sureBtn.bounds;
    layer.cornerRadius = 20;
    layer.colors = @[(id)[CXSinglerModel colorWithHex:@"#FF7F5A" alpha:1].CGColor,(id)[CXSinglerModel colorWithHex:@"#FE4438" alpha:1].CGColor];
    layer.startPoint = CGPointZero;
    layer.endPoint = CGPointMake(1, 0);
    [sureBtn.layer addSublayer:layer];
    [sureBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = 20;
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:sureBtn];
}

-(void)MessageAsk{
    if (_timeButton.selected) {
        return;
    }
    
    WEAKSELF;
    if ([Utils isTelephone:_PhoneField.text]) {
        _timeButton.selected = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
           [SVProgressHUD showWithStatus:@"加载中..."];
        });
        NetWorkModel *netModel = [[NetWorkModel alloc] init];
        netModel.sendMessage = ^(NSString *result) {
             dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
             });
            if ([result isEqual:@"success"]) {
                [SVProgressHUD showSuccessWithStatus:@"短信请求成功,请注意查收"];
                weakSelf.timeNum = 60;
                [weakSelf timeon];
            }
            else{
                 dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:result];
                 });
            }
        };
        [netModel getMessageCodeByPhone:_PhoneField.text];
    }
}

-(void)timeon{
    if (_timeNum == 0) {
        [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        return;
    }
    NSString *title = [NSString stringWithFormat:@"%lds冷却中",_timeNum];
    [_timeButton setTitle:title forState:UIControlStateNormal];
    _timeNum --;
    [self performSelector:@selector(timeNum) withObject:nil afterDelay:1];
}

-(void)viewLoad{
#pragma mark--fieldLoad
    NSArray *imagenames = @[@"phone",@"yanzheng",@"pass"];
    NSArray *placeholders = @[@"输入帐号或手机号",@"请输入收到的验证码",@"6-12位数字、字符或字母"];
    CGFloat sidewid = (cx_wid - 246) / 2;
    for (NSInteger index = 0; index < 3; index ++) {
        //        CGFloat sidewid = (cx_wid - 246) / 2;
        UIView*partView = [[UIView alloc] initWithFrame:CGRectMake(sidewid, cxsingler.topdis + 104 - 35 + 54 * index, cx_wid - sidewid * 2, 35)];
        
        UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 25, 25)];
        logo.image = [UIImage imageNamed:imagenames[index]];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(31, 7, 125, 21)];
        if (index == 1) {
            field.frame = CGRectMake(31, 7, 140, 21);
            _CodeFeild = field;
        }
        else if (index == 2){
            field.frame = CGRectMake(31, 7, 175, 21);
            _PassFeild = field;
        }
        else if (index == 0){
            [field becomeFirstResponder];
            _PhoneField = field;
        }
        field.font = [UIFont systemFontOfSize:15];
        field.placeholder = placeholders[index];
        
        UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 33, 246, 2)];
        layerView.backgroundColor = [CXSinglerModel colorWithHex:@"EEEFF1" alpha:1];
        
        [partView addSubview:logo];
        [partView addSubview:field];
        [partView addSubview:layerView];
        
        if (index == 0) {
            UIButton *messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(246 - 80, 5, 80, 25)];
            CAGradientLayer *layer = [[CAGradientLayer alloc] init];
            layer.frame = messageBtn.bounds;
            layer.colors = @[(id)[CXSinglerModel colorWithHex:@"#FF7F5A" alpha:1].CGColor,(id)[CXSinglerModel colorWithHex:@"#FE4438" alpha:1].CGColor];
            layer.startPoint = CGPointZero;
            layer.endPoint = CGPointMake(1, 0);
            [messageBtn.layer addSublayer:layer];
            [messageBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [messageBtn addTarget:self action:@selector(MessageAsk) forControlEvents:UIControlEventTouchUpInside];
            _timeButton = messageBtn;
            _timeButton.selected = NO;
            [messageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            messageBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            [partView addSubview:messageBtn];
        }
        [self.view addSubview:partView];
    }
    
    
    
    _rulebutton = [[UIButton alloc] initWithFrame:CGRectMake(sidewid, 312, 16, 16)];
    _rulebutton.layer.borderWidth = 2;
    [_rulebutton addTarget:self action:@selector(ruleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _rulebutton.layer.borderColor = [CXSinglerModel colorWithHex:@"EEEFF1" alpha:1].CGColor;
    [_rulebutton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_rulebutton setImage:[UIImage imageNamed:@"成功"] forState:UIControlStateSelected];
    
    UIButton *DetailRuleBtn = [[UIButton alloc] initWithFrame:CGRectMake(sidewid + 21, 307, 180, 27)];
    [DetailRuleBtn setTitle:@"我已同意《积分联盟使用协议》" forState:UIControlStateNormal];
    DetailRuleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [DetailRuleBtn setTitleColor:[CXSinglerModel colorWithHex:@"#8E8C8C" alpha:1] forState:UIControlStateNormal];
    DetailRuleBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    DetailRuleBtn.titleLabel.textColor = [CXSinglerModel colorWithHex:@"8E8C8C" alpha:1];
    [DetailRuleBtn addTarget:self action:@selector(detailRuled:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_rulebutton];
    [self.view addSubview:DetailRuleBtn];
}

#pragma mark--点击
-(void)detailRuled:(UIButton *)sender{
    _rulebutton.selected = YES;
    
}

-(void)ruleBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
